import allure
from playwright.sync_api import Page

class LoginPage:
    def __init__(self, page: Page):
        self.page = page
        self.locator_username = self.page.get_by_label("用 户 名:")
        self.locator_password = self.page.get_by_label("密     码:")
        self.locator_register_link = self.page.get_by_text('没有账号？点这注册')
        self.locator_login_btn = self.page.get_by_text("立即登录 > ")
        #用户输入框提示语
        self.locator_username_tip1 = self.page.locator("[data-fv-validator='notEmpty'][data-fv-for='username']")
        self.locator_username_tip2 = self.page.locator("[data-fv-validator='stringLength'][data-fv-for='username']")
        self.locator_username_tip3 = self.page.locator("[data-fv-validator='regexp'][data-fv-for='username']")
        #密码输入框提示语
        self.locator_password_tip1 = self.page.locator("[data-fv-validator='notEmpty'][data-fv-for='password']")
        self.locator_password_tip2 = self.page.locator("[data-fv-validator='stringLength'][data-fv-for='password']")
        self.locator_password_tip3 = self.page.locator("[data-fv-validator='regexp'][data-fv-for='password']")
        #账号或密码不正确
        self.locator_login_error = self.page.get_by_text('账号或密码不正确！')

    def navigate(self):
        with allure.step("导航到登录页面"):
            self.page.goto("/login.html")

    def fill_username(self, username):
        with allure.step("输入用户名"):
            self.locator_username.fill(username)

    def fill_password(self, password):
        with allure.step("输入密码"):
            self.locator_password.fill(password)

    def click_register_link(self):
        with allure.step("点击注册链接"):
            self.locator_register_link.click()

    def click_login_btn(self):
        with allure.step("点击登录按钮"):
            self.locator_login_btn.click()

    def login(self,username,password):
        self.locator_username.fill(username)
        self.locator_password.fill(password)
        self.locator_login_btn.click()
